#include "systemtray.h"
#include <QMessageBox>

systemTray::systemTray(QWidget *p)
{
    parent = p;
    tray = new QSystemTrayIcon( this );
    tray->setIcon( QIcon(":/cta/icons/cta.png") );
    connect( tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
             this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)) );
    tray->show();

    aboutAct = new QAction("About", this);
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(showAbout()) );

    contextMenu = new QMenu();
    contextMenu->addAction(aboutAct);
    tray->setContextMenu(contextMenu);
}

void systemTray::trayActivated( QSystemTrayIcon::ActivationReason reason )
{
    if( reason == QSystemTrayIcon::Trigger)
        toggleVisibility();
}

void systemTray::toggleVisibility()
{
    if( parent->isHidden() )
    {
        parent->show();
        if( parent->isMinimized())
        {
            if( parent->isMaximized())
                parent->showMaximized();
            else
                parent->showNormal();
        }
        parent->raise();
        parent->activateWindow();
    }
    else
    {
        parent->hide();
    }
}

void systemTray::showAbout()
{
    QMessageBox::about(parent, QString("About"),
                       QString("<center><font size=5><b><u>catchthealien-server</u></b></font><br/></center>")+
                       QString("<b>Version: 0.9<br/>")+
                       QString("E-mail: falsecam@googlemail.com<br/>")+
                       QString("Website: http://catchthealien.googlecode.com<br/>") );
}
